<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="借款额度">
        <el-input-number v-model="obj.borrowAmount" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间开始">
        <el-input-number v-model="obj.integralStart" :min="0" />
      </el-form-item>
      <el-form-item label="积分区间结束">
        <el-input-number v-model="obj.integralEnd" :min="0" />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate()"
        >
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
  <script>
    // 引入api模块
    import integralGradeApi from '@/api/core/integral-grade'

    export default{
      data () {
        return {
          saveBtnDisabled:false,// 是否禁用保存按钮，防止表单重复提交
          obj:{}// 积分等级对象
        }
      },

      created () {
        // 当路由中id存在时调用该方法(数据回显/修改回显数据)
        if(this.$route.params.id){
          this.fetchById(this.$route.params.id);
        }
      },

      methods: {
        fetchById(id){
          integralGradeApi.getById(id).then(res => {
            this.obj = res.data.info;
          })
        },

        // 新增或修改
        saveOrUpdate(){
          // 禁用保存按钮
          this.saveBtnDisabled=true;
          if(!this.obj.id){
            // 调用新增
            this.save();
          }else{
          // 调用修改
          this.update()
          }
        },

        save(){
          integralGradeApi.save(this.obj).then(res => {
            this.$message.success(res.message);
            this.$router.push('/core/integral-grade/list')
          })
        },

        update(){
          this.obj.createTime=null;
          this.obj.updateTime=null;
          console.log(this.obj);
          integralGradeApi.updateById(this.obj).then(res => {
            
            this.$message.success(res.message);
            this.$router.push('/core/integral-grade/list')
          })
        }
      }
    }
  </script>